//
//  SHTipLabelView.swift
//  shaohuo
//
//  Created by vicoo on 16/5/4.
//  Copyright © 2016年 lchenc3. All rights reserved.
//

import UIKit

@IBDesignable
class SHTipLabelView: UIView {

    @IBInspectable var textColor: UIColor!
    @IBInspectable var leftText: String! = "感谢费"
    @IBInspectable var rightText: String! {
        didSet {
            self.rightTitleLabel?.text = self.rightText
        }
    }
    @IBInspectable var viewBackgroundColor: UIColor!
        /*
        {
        didSet {
            // 关闭隐式动画
//            CATransaction.begin()
//            CATransaction.setDisableActions(true)
            
//            self.layer.borderWidth = 1.0
//            self.layer.borderColor = self.viewBackgroundColor.CGColor
            
//            CATransaction.commit()
        }
    }*/
    
    fileprivate var leftTitleLabel: UILabel?
    fileprivate var rightTitleLabel: UILabel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.setupSubView()
    }
    
    required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
        
        self.setupSubView()
    }
    /**
     设置子控件
     */
    func setupSubView() -> Void {
        leftTitleLabel = UILabel(frame: CGRect.zero)
        leftTitleLabel?.textColor = UIColor.white
        addSubview(leftTitleLabel!)
        
        rightTitleLabel = UILabel(frame: CGRect.zero)
        addSubview(rightTitleLabel!)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let leftLabelWidth = self.bounds.size.width / 5
        let viewHeight = self.bounds.size.height
        
        self.leftTitleLabel?.frame = CGRect(x: 0, y: 0, width: leftLabelWidth*2, height: viewHeight)
        self.leftTitleLabel?.font = UIFont.systemFont(ofSize: 12)
        self.leftTitleLabel?.textColor = UIColor.white
        self.leftTitleLabel?.text = self.leftText
        self.leftTitleLabel?.textAlignment = NSTextAlignment.center
        self.leftTitleLabel?.backgroundColor = self.viewBackgroundColor
        
        
        self.rightTitleLabel?.frame = CGRect(x: leftLabelWidth*2, y: 0, width: leftLabelWidth*3, height: viewHeight)
        self.rightTitleLabel?.font = UIFont.systemFont(ofSize: 12)
        self.rightTitleLabel?.textColor = self.viewBackgroundColor
        self.rightTitleLabel?.text = self.rightText
        self.rightTitleLabel?.textAlignment = NSTextAlignment.center
        

        self.setNeedsDisplay()
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        
        let context = UIGraphicsGetCurrentContext()
        
        let path: CGPath = CGPath(rect: self.bounds, transform: nil)
        context!.addPath(path)
        context!.setStrokeColor(self.viewBackgroundColor.cgColor)
        context!.setLineWidth(1.0)
        context!.setLineCap(CGLineCap.square)
        context!.strokePath()
    }
 
}
